package com.rupertjones.globalcron.web.init;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.io.InputStream;
import java.util.Properties;

public class InitServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(InitServlet.class);

    public void init() throws ServletException {
        super.init();
        String version = getVersion();
        getServletContext().setAttribute("version", version);
        getServletContext().setAttribute("started", new DateTime().toString("dd/MM/yyyy hh:mm:ss (ZZZ)"));
    }

    private String getVersion() {
        try {
            InputStream inputStream = getServletContext().getResourceAsStream("/META-INF/maven/com.rupertjones.global-cron/global-cron-web/pom.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            return (String) properties.get("version");
        } catch (Throwable th) {
            LOG.warn("Could not read manifest file.");
            return "UNKNOWN VERSION";
        }
    }
}
